package com.victualler.core.config.permission.service.impl;

import com.victualler.core.base.service.impl.BaseServiceImpl;
import com.victualler.core.config.menu.dto.MenuDto;
import com.victualler.core.config.menu.service.MenuService;
import com.victualler.core.config.permission.dto.UserDto;
import com.victualler.core.config.permission.entity.PermissionInfoDao;
import com.victualler.core.config.permission.entity.RoleInfoDao;
import com.victualler.core.config.permission.entity.UserInfoDao;
import com.victualler.core.config.permission.repository.UserInfoQueryRepository;
import com.victualler.core.config.permission.repository.UserInfoRepository;
import com.victualler.core.config.permission.service.PermissionInfoService;
import com.victualler.core.config.permission.service.RoleInfoService;
import com.victualler.core.config.permission.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: Guoshichang
 * @Date: Create in 17:03 2018/4/5
 * @Discription:  Dict字典类型对应的服务层
 */
@Service
public class UserInfoServiceImpl extends BaseServiceImpl<UserInfoDao,UserInfoRepository,UserInfoQueryRepository> implements UserInfoService {
    //角色服务
    @Autowired
    private RoleInfoService roleInfoService;
    //权限服务
    @Autowired
    private PermissionInfoService permissionInfoService;
    //菜单服务
    @Autowired
    private MenuService menuService;

    /**
     * @author Guoshichang
     * @since 19:04 on 2018/7/5.
     * @param userId 用户id
     * @Function  获取用户id
     */
    @Override
    public UserInfoDao getByUserId(String userId) {
        List<UserInfoDao> results = this.baseRepository.getByUserId(userId);
        if(results != null && results.size()>0){
            //隐藏密码
//            results.get(0).setPassword(null);
            return results.get(0);
        }
        return  null;
    }

    /**
     * @author Guoshichang
     * @since 19:04 on 2018/7/5.
     * @param userId 用户id
     * @Function 是否存在用户
     */
    @Override
    public boolean isHaveUser(String userId) {
        return this.getByUserId(userId)!=null;
    }

    @Override
    public UserDto getUserDtoByUserId(String userId) {
        //用户信息
        UserInfoDao  userInfoDao = this.getByUserId(userId);
        //角色列表
        List<RoleInfoDao> roleInfoList = roleInfoService.getRoleInfoByUserId(userInfoDao.getId());
        //权限列表
        List<PermissionInfoDao> permissionInfoList = permissionInfoService.getPermissionInfoByRoleId(roleInfoList);
        //菜单列表
        List<MenuDto> menuList = menuService.getMenuListByRoles(roleInfoList);
        //转换菜单为树结构
        //MenuDto menuDto = menuService.chengeToMenuDto(menuList);
        //封装大对象
        UserDto userDto = new UserDto();
        userDto.setUserInfoDao(userInfoDao);
        userDto.setPermissionInfoList(permissionInfoList);
        userDto.setRoleInfoDaoList(roleInfoList);
        //userDto.setMenuDto(menuDto);
        return userDto;
    }
}
